#include "loginview.h"
#include "ui_loginview.h"
#include "maincontroller.h"
#include "sqlcontroller.h"
#include <QDebug>


LoginView::LoginView(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LoginView)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("登录界面"));
    //
    MainController::getInstance()->initLoginView(this);

    //
    if (false == SqlController::getInstance()->initDataBase())
    {
        return;
    }
}

LoginView::~LoginView()
{
    delete ui;
}

void LoginView::on_btn_quit_clicked()
{
    exit(0);
}

void LoginView::on_btn_login_clicked()
{
    QString strUsername = ui->lineEdit_username->text();
    QString strPassword = ui->lineEdit_password->text();
    //
    if(SqlController::getInstance()->goLogin(strUsername, strPassword))
    {
        qDebug() << "login success";
        //登录成功跳转到主界面
        MainController::getInstance()->hideLoginView();
        MainController::getInstance()->showMainView();
    }
    else
    {
        qDebug() << "login failed";
    }

}

void LoginView::on_btn_register_clicked()
{
    QString strUsername = ui->lineEdit_username->text();
    QString strPassword = ui->lineEdit_password->text();
    //
    if(SqlController::getInstance()->goRegister(strUsername, strPassword))
    {
        qDebug() << "reg success";
    }
    else
    {
        qDebug() << "reg failed";
    }

}

